package com.uduemc.biso.master.backend.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.uduemc.biso.core.common.dto.CBNodemanageSiteList;
import com.uduemc.biso.core.common.extities.CHostData;
import com.uduemc.biso.core.extities.center.Host;
import com.uduemc.biso.core.extities.center.SysHostType;
import com.uduemc.biso.core.extities.center.SysServer;
import com.uduemc.biso.core.utils.JsonResult;
import com.uduemc.biso.master.backend.service.HostService;
import com.uduemc.biso.master.backend.service.ServermanageService;

/**
 * 站点管理模块
 * 
 * @author guanyi
 *
 */
@Controller
@RequestMapping("/backend/nodemanage")
public class BNodemanageController {

	private final static Logger logger = LoggerFactory.getLogger(BNodemanageController.class);

	@Autowired
	private HostService hostServiceImpl;

	@Autowired
	private ServermanageService servermanageServiceImpl;

	@Autowired
	private ObjectMapper objectMapper;

	/**
	 * 所有分控的站点列表管理
	 * 
	 * @param modelAndView
	 * @return
	 * @throws IOException
	 * @throws JsonProcessingException
	 * @throws JsonMappingException
	 * @throws JsonParseException
	 */
	@GetMapping(value = { "/sitelist" })
	public ModelAndView siteList(ModelAndView modelAndView)
			throws JsonParseException, JsonMappingException, JsonProcessingException, IOException {
		List<SysServer> serverByTypeStatus = servermanageServiceImpl.getServerByTypeStatus(0, (short) -1);
		List<SysHostType> hostType = hostServiceImpl.getHostType();

		modelAndView.addObject("listHostType", hostType);
		modelAndView.addObject("listHostTypeJson", objectMapper.writeValueAsString(hostType));
		modelAndView.addObject("listServer", serverByTypeStatus);
		modelAndView.addObject("listServerJson", objectMapper.writeValueAsString(serverByTypeStatus));
		modelAndView.setViewName("backend/nodemanage/sitelist");
		return modelAndView;
	}

	/**
	 * 所有分控的站点域名审核
	 * 
	 * @param modelAndView
	 * @return
	 * @throws IOException
	 * @throws JsonProcessingException
	 * @throws JsonMappingException
	 * @throws JsonParseException
	 */
	@GetMapping(value = { "/sitedomainvalid" })
	public ModelAndView sitedomainvalid(ModelAndView modelAndView)
			throws JsonParseException, JsonMappingException, JsonProcessingException, IOException {
		modelAndView.setViewName("backend/nodemanage/sitedomainvalid");
		return modelAndView;
	}

	/**
	 * 所有分控的站点制作完成审核
	 * 
	 * @param modelAndView
	 * @return
	 * @throws IOException
	 * @throws JsonProcessingException
	 * @throws JsonMappingException
	 * @throws JsonParseException
	 */
	@GetMapping(value = { "/sitevalid" })
	public ModelAndView sitevalid(ModelAndView modelAndView)
			throws JsonParseException, JsonMappingException, JsonProcessingException, IOException {
		modelAndView.setViewName("backend/nodemanage/sitevalid");
		return modelAndView;
	}

	/**
	 * 请求获取数据列表
	 * 
	 * @param cBNodemanageSiteList
	 * @return
	 * @throws JsonParseException
	 * @throws JsonMappingException
	 * @throws JsonProcessingException
	 * @throws IOException
	 */
	@PostMapping(value = { "/sitelist/infos" })
	@ResponseBody
	public JsonResult siteListInfos(@Valid @RequestBody CBNodemanageSiteList cBNodemanageSiteList)
			throws JsonParseException, JsonMappingException, JsonProcessingException, IOException {
		logger.info("username : ", cBNodemanageSiteList.getUsername());
		logger.info("typeId : ", cBNodemanageSiteList.getTypeId());
		logger.info("serverId : ", cBNodemanageSiteList.getServerId());
		logger.info("status : ", cBNodemanageSiteList.getStatus());
		logger.info("review : ", cBNodemanageSiteList.getReview());
		logger.info("trial : ", cBNodemanageSiteList.getTrial());
		logger.info("pageNumber : ", cBNodemanageSiteList.getPageNumber());
		logger.info("createAtFrom : ", cBNodemanageSiteList.getCreateAtFrom());
		logger.info("createAtTo : ", cBNodemanageSiteList.getCreateAtTo());
		logger.info("endAtFrom : ", cBNodemanageSiteList.getEndAtFrom());
		logger.info("endAtTo : ", cBNodemanageSiteList.getEndAtTo());

		String orderByString = cBNodemanageSiteList.getOrderByString();
		logger.info("orderByString : ", orderByString);

		if (StringUtils.hasText(orderByString) && orderByString.equals("1")) {
			// 顺序
			cBNodemanageSiteList.setOrderByString("`host`.`id` ASC");
		} else if (StringUtils.hasText(orderByString) && orderByString.equals("2")) {
			cBNodemanageSiteList.setOrderByString("`host`.`id` DESC");
		}

		cBNodemanageSiteList.setAgentId(0);
		PageInfo<CHostData> hostDataList = hostServiceImpl.getHostDataList(cBNodemanageSiteList);
		return JsonResult.ok(hostDataList);
	}

	/**
	 * 通过参数更新 host 数据中的 status 字段
	 * 
	 * @param hostId
	 * @param status
	 * @return
	 * @throws JsonParseException
	 * @throws JsonMappingException
	 * @throws JsonProcessingException
	 * @throws IOException
	 */
	@PostMapping(value = { "/sitelist/update-one-status" })
	@ResponseBody
	public JsonResult siteListUpdateOneStatus(@RequestParam("hostId") long hostId, @RequestParam("status") short status)
			throws JsonParseException, JsonMappingException, JsonProcessingException, IOException {
		Host host = hostServiceImpl.getHost(hostId);
		if (host == null) {
			return JsonResult.illegal();
		}
		if (host.getStatus() != null && host.getStatus().shortValue() == status) {
			return JsonResult.messageSuccess("更新成功！", host);
		}
		host.setStatus(status);
		Host updateHost = hostServiceImpl.updateHost(host);
		if (updateHost == null) {
			return JsonResult.assistance();
		}
		return JsonResult.messageSuccess("更新成功！", updateHost);
	}

	/**
	 * 通过参数更新 host 数据中的 end_at 字段
	 * 
	 * @param hostId
	 * @param endTime
	 * @return
	 * @throws IOException
	 * @throws JsonProcessingException
	 * @throws JsonMappingException
	 * @throws JsonParseException
	 */
	@PostMapping(value = { "/sitelist/update-one-endat" })
	@ResponseBody
	public JsonResult siteListUpdateOneEndAt(@RequestParam("hostId") long hostId, @RequestParam("endTime") Date endTime)
			throws JsonParseException, JsonMappingException, JsonProcessingException, IOException {
		Host host = hostServiceImpl.getHost(hostId);
		if (host == null) {
			return JsonResult.illegal();
		}
		host.setEndAt(endTime);
		Host updateHost = hostServiceImpl.updateHost(host);
		if (updateHost == null) {
			return JsonResult.assistance();
		}
		return JsonResult.messageSuccess("更新成功！", updateHost);
	}
}
